package base

import (
	"errors"
	"path"
)

//FileType 代表文件类型
type FileType uint8

const (
	JSON FileType = 0
	TOML FileType = 1
	YAM  FileType = 2
	YAML FileType = 3
)

var typeNames = []string{
	JSON: ".json",
	TOML: ".toml",
	YAM:  ".yam",
	YAML: ".yaml",
}

func (t FileType) Name() string {
	if int(t) < len(typeNames) {
		return typeNames[t]
	}
	return ""
}

func HasSuffix(fileName string) (FileType, error) {
	suffix := path.Ext(fileName)
	if len(suffix) == 0 {
		return 0, errors.New("FileType isn't exsit")
	}
	var hasSuffix bool
	var index FileType
	for index = 0; int(index) < len(typeNames); index++ {
		if suffix == typeNames[index] {
			hasSuffix = true
			break
		}
	}
	if !hasSuffix {
		return 0, errors.New("FileType is invalid")
	}
	return index, nil
}
